package com.fz.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by webrx on 2017/6/28.
 */
@Controller
public class Fileing {
    @RequestMapping("/up.action")
    public void upload(@RequestParam("file") CommonsMultipartFile f, HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String path=req.getServletContext().getRealPath("/upload/user/");
        /*String path = req.getServletContext().getRealPath("/upload/user/");*/
        File ff = new File(path);
        if(!ff.exists()){
            ff.mkdirs();
        }
        String aa =req.getParameter("name");
        System.out.println(path);
        System.out.println(f.getBytes());
        System.out.println(f.getContentType());
        System.out.println(f.getName());
        System.out.println(f.getOriginalFilename());

        System.out.println(aa);

        f.transferTo(new File(path,f.getOriginalFilename()));


    }
    @RequestMapping("upload02.do")
    public ModelAndView upload02(@RequestParam("file") List<CommonsMultipartFile> fs,HttpServletRequest req,HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        String path =req.getServletContext().getRealPath("/upload/book/");
        File ff= new File(path);
        if(!ff.exists()){
            ff.mkdirs();

        }
        ModelAndView m= new ModelAndView("kk");
        List<String> info = new ArrayList<String>();
        for(CommonsMultipartFile comf: fs){
            System.out.println(comf.getOriginalFilename());
            System.out.println(comf.getSize());
           // String ss01=comf.getOriginalFilename();
            // ss02=comf.getSize();
            comf.transferTo(new File(path,comf.getOriginalFilename()));

            info.add(String.format("文件名：%s,大小: %d",comf.getOriginalFilename(),comf.getSize()));
            m.addObject("fs",info);
            m.addObject("name",req.getParameter("name"));
        }
        return m;

    }
}
